package view;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.persistence.Inheritance;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;

import util.HibernateUtil;
import util.QueryBuilder;
import bean.NetResources;

/**
 * @author mareshkau
 */
@ManagedBean(name="searchresultsview")
@RequestScoped
@Inheritance
public class SearchResultsView implements Paginator {

	@ManagedProperty(value="#{param.includeWords}")
	private String includeWords;
	@ManagedProperty(value="#{param.sizeMinFilter}")
	private String sizeMin;
	@ManagedProperty(value="#{param.sizeMaxFilter}")
	private String sizeMax;
	@ManagedProperty(value="#{param.orderBy}")
	private String orderBy;
	@ManagedProperty(value="#{param.orderDirection}")
	private String orderDirection;
	@ManagedProperty(value="#{param.downloadPath}")
	private String downloadPath;
	@ManagedProperty(value="#{param.currentPage}")
	private int currentPage;
	@ManagedProperty(value="50")
	private int rowsPerPage;
	@ManagedProperty(value="#{param.excludeWords}")
	private String excludeWords;
	
	private int numberOfItems=-1;
	
	/**
	 * @return the downloadPath
	 */
	public String getDownloadPath() {
		return downloadPath;
	}

	/**
	 * @param downloadPath the downloadPath to set
	 */
	public void setDownloadPath(String downloadPath) {
		this.downloadPath = downloadPath;
	}

	/**
	 * @return the orderDirection
	 */
	public String getOrderDirection() {

		return orderDirection;
	}

	/**
	 * @param orderDirection the orderDirection to set
	 */
	public void setOrderDirection(String orderDirection) {
			this.orderDirection=orderDirection;
	}
	
	public int getNumberOfItems() {
		if(numberOfItems==-1) {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = QueryBuilder.getListCriteria(session,this,false);
			criteria.setProjection(Projections.rowCount());
			numberOfItems = Integer.valueOf(criteria.uniqueResult().toString());
			session.close();
		}
		if(getCurrentPage()>=numberOfItems) {
			setCurrentPage(numberOfItems-getCurrentPage());
		}
		return numberOfItems;
	}
	
	/**
	 * @return the orderBy
	 */
	public String getOrderBy() {
		return orderBy==null?orderBy:orderBy.trim();
	}

	/**
	 * @param orderBy the orderBy to set
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	/**
	 * @return the sizeMin
	 */
	public String getSizeMin() {
		return sizeMin;
	}

	/**
	 * @param sizeMin the sizeMin to set
	 */
	public void setSizeMin(String sizeMin) {
		this.sizeMin = sizeMin;
	}

	/**
	 * @return the sizeMax
	 */
	public String getSizeMax() {
		return sizeMax;
	}

	/**
	 * @param sizeMax the sizeMax to set
	 */
	public void setSizeMax(String sizeMax) {
		this.sizeMax = sizeMax;
	}
	
	public List<NetResources> getResources() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = QueryBuilder.getListCriteria(session,this,true);
		criteria.setFirstResult(getCurrentPage());
		criteria.setMaxResults(getRowsPerPage());
		criteria.setTimeout(500);
		List<NetResources> result = criteria.list();
		session.close();
		return result;
	}

	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		if(currentPage<0) {
			
			currentPage=0;
		}
		return currentPage;
	}

	/**
	 * @param currentPage the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * @return the rowsPerPage
	 */
	public int getRowsPerPage() {
		return rowsPerPage;
	}

	/**
	 * @param rowsPerPage the rowsPerPage to set
	 */
	public void setRowsPerPage(int rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}

	/**
	 * @param includeWords the includeWords to set
	 */
	public void setIncludeWords(String includeWords) {
		this.includeWords = includeWords;
	}

	/**
	 * @return the includeWords
	 */
	public String getIncludeWords() {
		return includeWords;
	}

	/**
	 * @param excludeWords the excludeWords to set
	 */
	public void setExcludeWords(String excludeWords) {
		this.excludeWords = excludeWords;
	}

	/**
	 * @return the excludeWords
	 */
	public String getExcludeWords() {
		return excludeWords;
	}
}